#include "top.h"
#include <QLabel>
#include <QApplication>
#include <QPainter>
#include <QImage>
#include <QPushButton>
#include <QIcon>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QTableWidget>
#include <QHeaderView>
#include <QTableWidgetItem>
#include <QDebug>
#include <QString>
#include <QStringList>

top::top(QWidget *parent):
    QWidget(parent),
    parent(parent)
{
    resize(parent->width(), parent->height());

    // 关闭按钮
    QPushButton* close_button = new QPushButton(this);
    QSize iconSize = QSize(width()*0.113, width()*0.05);
    close_button->setIconSize(iconSize);
    close_button->setIcon(QIcon(":/img/topUI/close.png"));
    close_button->move(width()*0.65, height()*0.36);
    close_button->resize(iconSize);

    connect(close_button, &QPushButton::clicked, this, [=](){
        close();
    });

    // 排行版表
    QString table_style = "QTableWidget{background-color: rgba(0, 0, 0, 0);\
                            border: 0px;}\
                            QHeaderView::section{background-color: rgba(0, 0, 0, 0);}";
    QTableWidget *top_table = new QTableWidget(this);

    // 初始化行列数、表头、大小、位置
    top_table->setColumnCount(2);
    top_table->setRowCount(5);
    top_table->setHorizontalHeaderLabels(QStringList() << "排名" << "分数");
    top_table->setStyleSheet(table_style);
    top_table->move(width()*0.26, height()*0.43);
    top_table->resize(width()*0.5, height()*0.4);
    top_table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    top_table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


    // 拉伸列名、隐藏表格线、隐藏行号
    top_table->horizontalHeader()->setStretchLastSection(true);
    top_table->setShowGrid(false);
    QHeaderView* headerView = top_table->verticalHeader();
    headerView->setHidden(true);

    // 显示数据
    QSqlDatabase database;

    if(QSqlDatabase::contains("qt_sql_default_connection"))
        database = QSqlDatabase::database("qt_sql_default_connection");
    else
        database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("MyDataBase.db");
    database.open();

    QSqlQuery sql_query;
    sql_query.exec("CREATE TABLE TOP(\
       ID INTEGER PRIMARY KEY AUTOINCREMENT,\
       score           INT NOT NULL\
        );");

    QString query = "select * from TOP order by score DESC;";
    sql_query.exec(query);

    int row = 0;
    while(sql_query.next())
    {
        int score = sql_query.value(1).toInt();
        top_table->setItem(row, 0, new QTableWidgetItem(QString::number(row+1)));
        top_table->setItem(row, 1, new QTableWidgetItem(QString::number(score)));
        for (int i = 0; i < 2; i++) {
            top_table->item(row, i)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            top_table->setSelectionMode(QAbstractItemView::NoSelection);
        }
        row++;
        if (row == 5) break;
    }
    database.close();

    // 测试数据
//    top_table->setItem(0, 0, new QTableWidgetItem("asdsd"));
//    top_table->item(0, 0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}

void top::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    // 模糊背景
    this->setAutoFillBackground(true);//这个语句很重要，没有的话，子widget背景色修改不成功
    QPalette myPalette;
    myPalette.setBrush(backgroundRole(),QColor(0,0,0,125));
    this->setPalette(myPalette);

    // 排行榜的背景图
    int width = parent->width();
    int height = parent->height();
    int x = width*0.1;
    int y = height*0.25;
    painter.drawImage(x, y,\
                      QImage(":/img/topUI/top.png").scaled(width*0.8, height*0.6));
}

top::~top()
{

}
